package com.cgx.dao;

import com.cgx.entity.VersionLock;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author chenguanxin
 * @date 2020-08-27 22:47
 */
@Repository
public interface VersionLockDao  extends JpaRepository<VersionLock, String> {

    @Transactional
    @Modifying
    @Query(value = "UPDATE version_lock " +
            "SET `value` = ?2, " +
            "`version` = ?3 " +
            "WHERE id = ?1 " +
            "AND `version` = ?4", nativeQuery = true)
    int updateVersionLockByVersion(String id, String newValue, String newVersion, String oldVersion);

    @Transactional
    @Modifying
    @Query(value = "UPDATE version_lock " +
            "SET `value` = ?2 " +
            "WHERE id = ?1 ", nativeQuery = true)
    int updateVersionLockValueById(String id, String value);
}
